<?php
namespace app\index\controller;
use app\index\model\UserModel;

class Wx extends BaseController
{
	const app_id = '';
    public function get_user_id()
    {
    	$code = request()->param('code');

        if(empty($code))
    	{
    		return 'error_code';
    	}
    	$serverUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxe6912c70b13ee43a&secret=44eae6668462b03fcfb07f474d403943&js_code='.$code.'&grant_type=authorization_code';
		$html = file_get_contents($serverUrl);
        $html = json_decode($html, true);
        trace("check wx login request:".json_encode(request()), 'debug');// $html;
		trace("check wx login:".json_encode($html), 'debug');// $html;
        $playerId = $html['openid'];
        if(empty($playerId))
        {
            return 'error_player_id';
        }
        // load from db
        $playerData = UserModel::getUserData($playerId);
        trace("check wx login:".json_encode($playerData), 'debug');// $html;
        return $this->out($playerData);
    }

}
